package com.hmyd.hmyd_android.view.ad;

import java.util.ArrayList;

import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.view.CustomImageView;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


/**
 * 自动轮播适配器
 * 
 **/
public class AutoGalleryAdapter extends BaseAdapter {

	public ArrayList<AutoGalleryBase> mAdBase = new ArrayList<AutoGalleryBase>();
	private AsyncImageLoader asyncImageLoader = null;
	private LayoutInflater mInflater;
	private AutoGallery mAutoGallery;
	private Activity mContext;
	private int mWidth;
	private int mHeight;
	protected boolean mIsLoop = true;// 循环播放

	public AutoGalleryAdapter(Activity mContext, AutoGallery mAutoGallery,
			ArrayList<AutoGalleryBase> mAdBase) {
		// TODO Auto-generated constructor stub
		this.mAutoGallery = mAutoGallery;
		this.mContext = mContext;
		this.mAdBase = mAdBase;
		mInflater = LayoutInflater.from(mContext);
		asyncImageLoader = new AsyncImageLoader(mContext,
				mAutoGallery.getItemLoadingResID());
		if (mAutoGallery.getItemWidth() > 0) {
			mWidth = MyLayout.getScreenWidth(mContext)
					* mAutoGallery.getItemWidth() / mAutoGallery.getItemRes();
			mHeight = mWidth * mAutoGallery.getItemHeight()
					/ mAutoGallery.getItemWidth();
		}

	}

	/**
	 * 循环播放设置
	 * */
	public void setLoop(boolean mIsLoop) {
		this.mIsLoop = mIsLoop;
		if (mAdBase != null && mAdBase.size() <= 1) {
			this.mIsLoop = false;
		}
	}

	/**
	 * 判断是否是循环标志
	 * */
	public boolean isLoop() {
		return mIsLoop;
	}

	/**
	 * 获取图片的宽度
	 * */
	public int getIconWidth() {
		return mWidth;
	}

	/**
	 * 获取图片的高度
	 * */
	public int getIconHeight() {
		return mHeight;
	}

	public LayoutInflater getLayoutInflater() {
		return mInflater;
	}

	public AsyncImageLoader getAsyncImageLoader() {
		return asyncImageLoader;
	}

	/**
	 * 加载图片
	 * */
	public void loadIcon(CustomImageView view, String url) {
		asyncImageLoader.load(url, view);
	}

	public int getListCount() {
		// TODO Auto-generated method stub
		return mAdBase.size();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (isLoop())
			return Integer.MAX_VALUE;
		return mAdBase.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;

		if (convertView == null) {
			convertView = mInflater.inflate(mAutoGallery.getItemLayout(), null);
			holder = new ViewHolder();
			holder.icon = (CustomImageView) convertView.findViewById(R.id.item_icon);
			holder.title = (TextView) convertView.findViewById(R.id.item_title);
			holder.icon.setClipSize(mWidth, mHeight);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		position = position % mAdBase.size();
		if (position < 0 || position >= mAdBase.size())
			return convertView;

		if (mAdBase.get(position).mIconID > 0) {
			holder.icon.setImageResource(mAdBase.get(position).mIconID);
		} else {
			String url = mAdBase.get(position).mUrl;
			loadIcon(holder.icon, url);
		}

		if (holder.title != null) {
			if (mAutoGallery.getPointViewWidth() != 0) {
				DisplayMetrics dm = new DisplayMetrics();
				mContext.getWindowManager().getDefaultDisplay().getMetrics(dm);
				holder.title.setMaxWidth(dm.widthPixels
						- mAutoGallery.getPointViewWidth());
			}
			if (mAdBase.get(position).mDrawableLeft != -1) {
				Drawable drawable = mContext.getResources().getDrawable(
						mAdBase.get(position).mDrawableLeft);
				drawable.setBounds(0, 0, drawable.getMinimumWidth(),
						drawable.getMinimumHeight());
				holder.title.setCompoundDrawables(drawable, null, null, null);
			}
			holder.title.setText(mAdBase.get(position).mTitle);
		}

		return convertView;
	}

	class ViewHolder {
		CustomImageView icon;
		TextView title;
	}

}
